home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 014 / dirhidn.arc / DIRHIDN.ASM next >
Encoding:
Assembly Source File  |  1984-07-10  |  8.5 KB  |  434 lines

  1.     page    61,132
  2.     title DIRHIDN
  3.  
  4. ; dirhidn.asm  24 Jan 86  Craig Milo Rogers at USC/ISI
  5. ;    Fixed the command line parsing logic to properly interpret
  6. ;    the case when the program is called without arguments.
  7. ;    Same for write_string().  Note:  there are still many holes
  8. ;    in command line parsing.
  9. ; dirhidn.asm  23 Jan 86  Craig Milo Rogers at USC/ISI
  10. ;    Created this program based on WHEREIS.ASM.
  11. ;
  12. ;    This program searches for "hidden" and "system"    files.
  13. ;
  14. ;        DIRHIDN path
  15. ;
  16. ;    searches for hidden or system files in the directory subtree
  17. ;    starting with "path".
  18.  
  19. dirhidn segment public
  20.     assume cs:dirhidn,ds:dirhidn
  21.  
  22. ;equates
  23.  
  24. COMMAND_LINE    EQU    80H + 1
  25. NULL        EQU    00H
  26. CR        EQU    0DH
  27. LF        EQU    0AH
  28. SPACE        EQU    ' '
  29. DOT        EQU    '.'
  30. STAR        EQU    '*'
  31. MAX_SCAN_LEN    EQU    64
  32. TERMINATE    EQU    20H
  33. HIDDEN        EQU    02H
  34. SYSTEM        EQU    04H
  35. DIRECTORY    EQU    10H
  36. PRINT_CHAR    EQU    02H
  37. SET_DTA        EQU    1AH
  38. SEARCH_FIRST    EQU    4EH
  39. SEARCH_NEXT    EQU    4FH
  40. DOS_CALL    EQU    21H
  41. PATH_SEPARATOR    EQU    '\'
  42.  
  43. ;macros
  44.  
  45. CLEAR    macro    reg
  46.     xor    reg,reg
  47.     endm
  48.  
  49. ;this is the format for the dos data transfer area used when dos 2.0
  50. ;searches for file match in directories
  51.  
  52. dta    struc
  53.     reserved    db    21 dup (?)
  54.     attribute    db    ?
  55.     time        dw    ?
  56.     date        dw    ?
  57.     size        dd    ?
  58.     name_found    db    13 dup (?)
  59. dta    ends
  60.  
  61.     org    100h
  62. main    proc    far
  63.  
  64. ;  this is the main program that sets up the initial conditions for
  65. ;  search_directory which in turn, does a recursive search.
  66. ;    reads:    command line
  67. ;    writes: path_name, file_name, file_attr
  68. ;    calls:    search_directory
  69. ;
  70.  
  71. mainline proc    near
  72.  
  73. start:
  74.     mov    si, offset star_name    ; Copy the *.* string to file_name.
  75.     mov    di, offset file_name
  76.     cld
  77. file_loop:
  78.     lodsb
  79.     stosb
  80.     or    al,al
  81.     jnz    file_loop
  82.     
  83.     mov    si, COMMAND_LINE        ;start of command line
  84.     mov    di, offset path_name
  85.     cld
  86.     lodsb                ; Get first char.
  87.     cmp    al,CR            ; Is it end-of-line?
  88.     je    terminate_name        ; Yes, default to current directory.
  89.                     ; Otherwise ignore char (space).
  90. get_search_name:
  91.     lodsb                ; Get next char.
  92.     cmp    al, CR            ; Is it a carriage return?
  93.      je    done_reading_name    ;   Yes, end of command line.
  94.     cmp    al, SPACE        ; Is it a space?
  95.      je    done_reading_name    ;   Yes, end of command arg.
  96.     stosb                ; Store another path char.
  97.     jmp    get_search_name        ;Loop --
  98. done_reading_name:
  99.     cmp    byte ptr [di-1],PATH_SEPARATOR
  100.     je    terminate_name
  101.     mov    al,PATH_SEPARATOR
  102.     stosb
  103. terminate_name:
  104.     CLEAR    al            ; terminate path
  105.     stosb
  106.  
  107.     mov    file_attr, HIDDEN or SYSTEM
  108.                     ; desired file attributes
  109.  
  110.     mov    di, offset path_name
  111.     CLEAR    al
  112.     cld
  113.     mov    cx, MAX_SCAN_LEN
  114.     repnz    scasb
  115.     mov    bx,di
  116.     dec    bx            ;ds:bx points to end of path_name
  117.     
  118.     mov    dx,NULL
  119.     call search_directory
  120.     int    TERMINATE
  121. mainline endp
  122.  
  123. ; this procedure searches all the files in the current directory
  124. ; looking for a match.    It also prints the full name for each match
  125. ; which is HIDDEN or SYSTEM.
  126. ;
  127. ;    ds:bx    pointer to end of current path name
  128. ;    ds:dx    old disk transfer area (dta)
  129. ;
  130. ; reads:    disk transfer area (dta)
  131. ;        file_attr
  132. ; writes:    disk transfer area (dta)
  133. ; calls        build_name, get_first_Match
  134. ;        write_matched_name, get_next_match
  135. ;        build_star_name, search_sub_directory
  136. ;
  137. search_directory proc    near
  138.  
  139.     push    si
  140.     push    dx
  141.     call    build_name
  142.     mov    cx,file_attr
  143.     call    get_first_match
  144.     jc    no_match                ;If no match --
  145.     mov    si,dx                ; Save pointer to new DTA.
  146.     test    [si].attribute,HIDDEN or SYSTEM
  147.     jz    find_next_file
  148.     call    write_matched_name
  149. find_next_file:
  150.     mov    cx,file_attr
  151.     call    get_next_match
  152.     jc    no_match
  153.     test    [si].attribute,HIDDEN or SYSTEM
  154.     jz    find_next_file
  155.     call    write_matched_name
  156.     jmp    find_next_file                ;Loop --
  157.  
  158. no_match:
  159.     pop    dx
  160.     push    dx
  161.     call    build_star_name
  162.     mov    cx,file_attr            ; Get the search attributes.
  163.     and    cx,HIDDEN or SYSTEM        ; Select interesting bits.
  164.     or    cx,DIRECTORY            ; Include directory searches.
  165.     call    get_first_match
  166.     jc    no_more_matches            ;If no match --
  167.     mov    si,dx                ; Save pointer to new DTA.
  168.     test    [si].attribute,DIRECTORY
  169.     jnz    is_directory            ;If directory entry --
  170. find_next_directory:
  171.     mov    cx,file_attr            ; Get the search attributes.
  172.     and    cx,HIDDEN or SYSTEM        ; Select interesting bits.
  173.     or    cx,DIRECTORY            ; Include directory searches.
  174.     call    get_next_match
  175.     jc    no_more_matches            ;If no more entries --
  176.     test    [si].attribute,DIRECTORY
  177.     jz    find_next_directory        ;If not a directory --
  178. is_directory:
  179.     cmp    [si].name_found,DOT
  180.     je    find_next_directory        ;If it's . or ..
  181.     call    search_sub_directory        ;search sub directory
  182.     push    ax
  183.     mov    ah,SET_DTA
  184.     int    DOS_CALL
  185.     pop    ax
  186.     jmp    find_next_directory
  187. no_more_matches:
  188.     pop    dx
  189.     pop    si
  190.     ret
  191.  
  192. search_directory  endp
  193. page
  194. ; This procedure searches the sub directory who's name is in dta
  195. ;
  196. ;    ds:bx    end of the current pathname
  197. ;    ds:[dx].name_found    name of subdirectory for search
  198. ;
  199. ; reads:    path_name
  200. ; writes:    path_name
  201. ; calls:    search_directory
  202. ;
  203.  
  204. search_sub_directory  proc  near
  205.  
  206.     push    di
  207.     push    si
  208.     push    ax
  209.     push    bx
  210.     cld
  211.     mov    si, dx
  212.     add    si, offset name_found
  213.     mov    di,bx
  214. copy_loop:
  215.     lodsb
  216.     stosb
  217.     or    al,al
  218.     jnz    copy_loop
  219.     mov    bx,di
  220.     std
  221.     stosb
  222.     mov    al,PATH_SEPARATOR
  223.     stosb
  224.     call    search_directory
  225.     pop    bx
  226.     mov    byte ptr [bx],NULL
  227.     pop    ax
  228.     pop    si
  229.     pop    di
  230.     ret
  231.  
  232. search_sub_directory  endp
  233. page
  234.  
  235. ; This procedure prints the matched name after the path name
  236. ;
  237. ;  ds:dx    pointer to current disk transfer area
  238. ;
  239. ; reads:    path_name, name_found (in dta)
  240. ; writes:    write_string, send_crlf
  241. ;
  242.  
  243. write_matched_name    proc    near
  244.  
  245.     push    ax
  246.     push    dx
  247.     mov    dx,offset path_name
  248.     mov    al,[bx]
  249.     mov    byte ptr [bx],NULL
  250.     call    write_string
  251.     mov    [bx],al
  252.     pop    dx
  253.     push    dx
  254.     add    dx, offset name_found
  255.     call    write_string
  256.     call    send_crlf
  257.     pop    dx
  258.     pop    ax
  259.     ret
  260. write_matched_name endp
  261.  
  262.  
  263. ;  This procedure builds an absolute search name from path_name
  264. ;  followed by file_name
  265. ;
  266. ;  reads:    file_name
  267. ;  calls:    build    (to build the name)
  268. ;
  269.  
  270. build_name    proc    near
  271.  
  272.     push    si
  273.     mov    si, offset file_name
  274.     call    build
  275.     pop    si
  276.     ret
  277. build_name    endp
  278.  
  279. build_star_name proc    near
  280.     push    si
  281.     mov    si, offset star_name
  282.     call    build
  283.     pop    si
  284.     ret
  285. build_star_name endp
  286.  
  287. page
  288. ; This procedure appends the string at ds:si to path_name in
  289. ; path_name.  It knows where the path name ends from knowing
  290. ; how long path_name is.
  291. ;
  292. ;    ds:si    name of file
  293. ;    ds:bx    end of path_name
  294. ;
  295. ; reads:    ds:si
  296. ; writes:    path_name
  297. ;
  298.  
  299. build    proc    near
  300.     push    ax
  301.     push    di
  302.     mov    di,bx
  303.     cld
  304. copy_name:
  305.     lodsb
  306.     stosb
  307.     or    al,al
  308.     jnz    copy_name            ;If not end of string yet --
  309.     pop    di
  310.     pop    ax
  311.     ret
  312. build    endp
  313.  
  314. ; This procedure find the first match between the name given by
  315. ; ds:dx and the directory entries found in the directory path_name
  316. ;
  317. ;    cx    file attributes for search
  318. ;    ds:dx    pointer to current disk transfer area
  319. ;
  320. ;  returns:
  321. ;    cf    0    a match was found
  322. ;        1    no match found
  323. ;    ax        error code returned
  324. ;        2    file not found
  325. ;        18    no more files
  326. ;    ds:dx        pointer to new disk transfer area
  327. ;
  328. ; reads:    path_name
  329. ; writes:    disk_transfer_areas
  330. ;
  331.  
  332. get_first_match proc    near
  333.  
  334.     cmp    dx,NULL
  335.     ja    allocate        ;go allocate space --
  336.     mov    dx, offset disk_transfer_areas-type dta
  337. allocate:
  338.     add    dx,type dta
  339.     mov    ah,SET_DTA
  340.     int    DOS_CALL
  341.     push    dx
  342.     mov    dx, offset path_name
  343.     mov    ah,SEARCH_FIRST            ;call for find first match
  344.     int    DOS_CALL
  345.     pop    dx
  346.     ret
  347. get_first_match endp
  348.  
  349.  
  350. ; This procedure is much like get_first_match
  351. ;
  352. ;    cx    file attributes for search
  353. ;
  354. ; returns:
  355. ;    cf    0    a match was found
  356. ;        1    no match found
  357. ;    ax        error code returned
  358. ;        2    file not found
  359. ;        18    no more files
  360. ;
  361. ; reads:    path_name
  362. ; writes:    disk_transfer_areas
  363. ;
  364.  
  365. get_next_match    proc    near
  366.     push    dx
  367.     mov    dx, offset path_name
  368.     mov    ah,SEARCH_NEXT
  369.     int    DOS_CALL
  370.     pop    dx
  371.     ret
  372. get_next_match    endp
  373.  
  374. ; This procedure sends a crlf pair of characters to the screen
  375. ;
  376.  
  377. send_crlf    proc    near
  378.  
  379.     push    ax
  380.     push    dx
  381.     mov    ah,PRINT_CHAR
  382.     mov    dl,CR
  383.     int    DOS_CALL
  384.     mov    dl,LF
  385.     int    DOS_CALL
  386.     pop    dx
  387.     pop    ax
  388.     ret
  389. send_crlf    endp
  390.  
  391. ; This procedure writes the asciiz string at
  392. ;  ds:dx    address of asciiz string
  393. ;
  394.  
  395. write_string    proc    near
  396.  
  397.     push    ax
  398.     push    dx
  399.     push    si
  400.     cld
  401.     mov    si,dx
  402.     mov    ah,PRINT_CHAR
  403. write_string_loop:
  404.     lodsb
  405.     or    al,al
  406.     jz    write_string_done
  407.     mov    dl,al
  408.     int    DOS_CALL
  409.     jmp    write_string_loop
  410. write_string_done:
  411.     pop    si
  412.     pop    dx
  413.     pop    ax
  414.     ret
  415. write_string    endp
  416.  
  417. ; This is the data storage area and must be the last thing
  418. ; in the program.
  419. ;
  420.  
  421. star_name    db    '*.*',NULL
  422. path_name    db    80 dup (0)    ;space for 64 char pathname and
  423.                     ;13 char filename
  424. file_name    db    13 dup (0)    ;save room for full dos filename
  425. file_attr    dw    ?        ; attribute value for searches.
  426.  
  427. disk_transfer_areas label byte        ;this must start at the end of dirhidn
  428.  
  429. main    endp
  430.  
  431. dirhidn    ends
  432.  
  433.     end    start
  434.